package com.javacv.plus.extensions.image.steps;

import com.javacv.plus.core.processor.ProcessorContext;
import com.javacv.plus.core.processor.ProcessorStep;
import com.javacv.plus.extensions.image.ImageProcessor;
import com.javacv.plus.extensions.image.ProcessorResult;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.Scalar;
import org.bytedeco.opencv.global.opencv_core;

/**
 * 边框添加步骤
 */
@Slf4j
public class BorderStep implements ProcessorStep<ImageProcessor, ProcessorResult> {
    private final int size;
    private final int r, g, b;

    public BorderStep(int size, int r, int g, int b) {
        this.size = size;
        this.r = r;
        this.g = g;
        this.b = b;
    }

    @Override
    public ProcessorResult execute(ImageProcessor processor, ProcessorContext context) throws Exception {
        Mat currentImage = context.getAttribute("currentImage");
        if (currentImage == null) {
            throw new IllegalStateException("当前图像为空");
        }

        log.debug("添加边框: size={}, color=({},{},{})", size, r, g, b);

        Mat result = new Mat();
        Scalar borderColor = new Scalar(b, g, r, 0); // OpenCV使用BGR格式
        
        opencv_core.copyMakeBorder(currentImage, result, size, size, size, size, 
                opencv_core.BORDER_CONSTANT, borderColor);

        context.setAttribute("currentImage", result);
        context.setAttribute("processedImage", result);

        log.debug("边框添加完成");
        return null;
    }

    @Override
    public String getStepName() {
        return "Border";
    }

    @Override
    public int getPriority() {
        return 90;
    }
} 